package com.megvii.koala.tv.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.util.DateTimeUtil;
import com.megvii.corelib.util.ScreenUtil;
import com.megvii.koala.base.KoalaFragment;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.presenter.WebPresenter;
import com.megvii.koala.tv.web.KoalaTvWebAPI;
import com.megvii.koala.tv.web.KoalaTvWebView;

import butterknife.Bind;

/**
 * Created by linyue on 16/1/24.
 */
public class WebFragment extends KoalaFragment
{
    @Bind(R.id.web_browser)
    protected KoalaTvWebView webView;

    @Bind(R.id.web_video_surface_local)
    protected SurfaceView videoSurfaceLocal;

    @Bind(R.id.web_video_surface_net)
    protected SurfaceView videoSurfaceNet;

    @Bind(R.id.web_video_container)
    protected RelativeLayout videoContainer;

    @Bind(R.id.web_seekbar_container)
    protected View seekBarContainer;

    @Bind(R.id.web_seekbar)
    protected SeekBar seekBar;

    @Bind(R.id.web_video_current_time)
    protected TextView videoCurrentTime;

    @Bind(R.id.web_video_duration_time)
    protected TextView videoDurationTime;

    @Bind(R.id.not_choose_file)
    protected RelativeLayout not_choose_file;

    private View rootView;

    private KoalaTvWebAPI webAPI;

    private float cachedLeft;

    private float cachedTop;

    private float cachedWidth;

    private float cachedHeight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.web_layout, container, false);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        webAPI = webView;
    }

    public WebPresenter getWebPresenter()
    {
        return (WebPresenter) presenter;
    }

    public void loadWeb(String url)
    {
        webView.loadUrl(url);
    }

    public void showVideo(boolean show)
    {
        videoContainer.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void resizeVideoContainer(float left, float top, float width, float height,
            boolean cachePosition, boolean changeVideoPosition)
    {
        if (cachePosition)
        {
            cachedLeft = left;
            cachedTop = top;
            cachedWidth = width;
            cachedHeight = height;
        }
        if (changeVideoPosition)
        {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoContainer
                    .getLayoutParams();
            params.width = (int) (width * ScreenUtil.getInstance().screenWidth);
            params.height = (int) (height * ScreenUtil.getInstance().screenHeight);
            params.leftMargin = (int) (left * ScreenUtil.getInstance().screenWidth);
            params.topMargin = (int) (top * ScreenUtil.getInstance().screenHeight);
            videoContainer.setLayoutParams(params);
            videoContainer.invalidate();
        }
    }

    public void recoverVideoContainerSize()
    {
        resizeVideoContainer(cachedLeft, cachedTop, cachedWidth, cachedHeight, false, true);
    }

    public void showWebView(boolean show)
    {
        webView.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showNotChooseFileLayout(boolean show){
        if(show) {
            not_choose_file.setVisibility(View.VISIBLE);
            videoSurfaceNet.setVisibility(View.GONE);
        } else {
            not_choose_file.setVisibility(View.GONE);
            videoSurfaceNet.setVisibility(View.VISIBLE);
        }
    }

    public SurfaceView getVideoSurfaceLocal()
    {
        return videoSurfaceLocal;
    }

    public SurfaceView getVideoSurfaceNet()
    {
        return videoSurfaceNet;
    }

    public void showSurfaceView(boolean local)
    {
        videoSurfaceLocal.setVisibility(local ? View.VISIBLE : View.GONE);
        videoSurfaceNet.setVisibility(local ? View.GONE : View.VISIBLE);
    }

    public KoalaTvWebAPI getWebAPI()
    {
        return webAPI;
    }

    public void setVideoDurationTime(int time)
    {
        String text = formatVideoTime(time);
        videoDurationTime.setText(text);
        seekBar.setMax(time);
    }

    public void setVideoCurrentTime(int time)
    {
        String text = formatVideoTime(time);
        videoCurrentTime.setText(text);
        seekBar.setProgress(time);
    }

    private String formatVideoTime(int time)
    {
        time = time / 1000;
        int minute = time / 60;
        int second = time % 60;
        String minuteString = DateTimeUtil.formatNumber(minute, 2);
        String secondString = DateTimeUtil.formatNumber(second, 2);
        return minuteString + ":" + secondString;
    }

    public void setSeekBarVisible(boolean visible)
    {
        //TODO 调试用进度条
        //        seekBarContainer.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @Override
    public Class<? extends KoalaPresenter> getPresenterClass()
    {
        return WebPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setWebViewListener(KoalaTvWebView.KoalaTvWebViewListener listener)
    {
        webView.setListener(listener);
    }
}
